'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 扣除资源相关
 * @author 454522799@qq.com (by xiaoxu)
 */

var _ = require('lodash');
var rewardType = require('../consts/rewardType');
var moduleType = require('../consts/moduleType');
var operationType = require('../consts/operationType');
var roleService = require('./roleService');
var Code = require('../consts/code.js');

// todo: 增加所需装入数据表检测
// 扣除多种资源
// 扣除格式：

//var list = [{
//cost_type: 1,
//discount_price: 20
//}, {
//cost_type: 2,
//discount_price: 20
//}];

exports.subResListToRole = function (player_manager, sub_list, module_type, operation_type, res_int_val, res_str_val) {
    module_type = module_type || moduleType.MDL_COMMON_REWARD;
    operation_type = operation_type || operationType.OPR_CMN_REWARD;
    var code = 0;
    _.each(sub_list, function (sub_item) {
        code = _subResToRole(player_manager, sub_item, module_type, operation_type, res_int_val, res_str_val);
        if (!!code) {
            return false;
        }
    });
    return code;
};

/**
 * 扣除单一资源
 *
 */
exports.subOnlyResToRole = function (player_manager, sub_item, module_type, operation_type, res_int_val, res_str_val) {
    module_type = module_type || moduleType.MDL_COMMON_REWARD;
    operation_type = operation_type || operationType.OPR_CMN_REWARD;
    return _subResToRole(player_manager, sub_item, module_type, operation_type, res_int_val, res_str_val);
};

// jshint maxstatements:150
var _subResToRole = function (player_manager, sub_item, module_type, operation_type, res_int_val, res_str_val) {
    var cost_type = sub_item.cost_type;
    var sub_num = sub_item.discount_price || 1;
    switch (cost_type) {
    case rewardType.GOLD:
        {
            if (!roleService.subGold(player_manager, module_type, operation_type, sub_num, res_int_val, res_str_val)) {
                console.error('error GOLD resources  not enough');
                return Code.USER_GOLD_NOT_ENOUGH;
            }
            break;
        }
    case rewardType.DIAMOND:
        {
            if (!roleService.subDiamond(player_manager, module_type, operation_type, sub_num, res_int_val, res_str_val)) {
                console.error('error DIAMOND resources  not enough');
                return Code.USER_DIAMOND_NOT_ENOUGH;
            }
            break;
        }
    case rewardType.FOOD:
        {
            if (!roleService.subFood(player_manager, module_type, operation_type, sub_num, res_int_val, res_str_val)) {
                console.error('error FOOD resources  not enough');
                return Code.USER_FOOD_NOT_ENOUGH;
            }
            break;
        }
    default:
        {
            console.error('cost type is wrong:' + cost_type);
            //break;
            return Code.USER_NOT_RESOURCES;
        }
    }
};
